package cn.itcast.crawler.jd.util;

import org.apache.http.client.CookieStore;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.BasicCookieStore;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
import org.apache.http.message.BasicHeader;
import org.apache.http.util.EntityUtils;
import org.springframework.stereotype.Component;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.UUID;

@Component
public class HttpUtils {
    private PoolingHttpClientConnectionManager cm;
    public HttpUtils(){
        this.cm=new PoolingHttpClientConnectionManager();
        cm.setMaxTotal(200);
        //设置每个主机的并发数
        cm.setDefaultMaxPerRoute(20);
    }

  public String  getHtml(String url) throws IOException {
      CookieStore cookieStore = new BasicCookieStore();
      CloseableHttpClient httpClient=
              HttpClients.custom().setDefaultCookieStore(cookieStore).setConnectionManager(cm).build();
      HttpGet httpGet=new HttpGet(url);
      httpGet.setHeader("Connection","keep-alive");
      httpGet.addHeader(new BasicHeader("user-agent", "Mozilla/5.0"));
      httpGet.setConfig(this.getConfig());
      CloseableHttpResponse response = null;
        try {
            response=httpClient.execute(httpGet);
            if(response.getStatusLine().getStatusCode()==200){
                String html="";
                if(response.getEntity()!=null){
                    html= EntityUtils.toString(response.getEntity());
                }
                return html;
            }

        }catch (Exception e){
            e.printStackTrace();
        }finally {
            try {
                if(response!=null){
                    response.close();
                }
            }catch (Exception e){
            e.printStackTrace();
            }
        }
        return null;

  }

  public String  getImage(String url){
      CookieStore cookieStore = new BasicCookieStore();
      CloseableHttpClient httpClient=
              HttpClients.custom().setDefaultCookieStore(cookieStore).setConnectionManager(cm).build();
      HttpGet httpGet=new HttpGet(url);
      httpGet.setHeader("Connection","keep-alive");
      httpGet.addHeader(new BasicHeader("user-agent", "Mozilla/5.0"));
      httpGet.setConfig(this.getConfig());
      CloseableHttpResponse response = null;

      try {
          response=httpClient.execute(httpGet);
          if(response.getStatusLine().getStatusCode()==200){
              // 获取文件类型
             String extName=url.substring(url.lastIndexOf("."));
            String imageName= UUID.randomUUID().toString()+extName;
              OutputStream outputStream=new FileOutputStream(new File("/Users/zhaoenguang/upload-files/"+imageName));
              response.getEntity().writeTo(outputStream);
              return imageName;
          }

      }catch (Exception e){
          e.printStackTrace();
      }finally {
          try {
              if(response!=null){
                  response.close();
              }
          }catch (Exception e){
              e.printStackTrace();
          }
      }
      return null;

  }
    private RequestConfig getConfig() {

        RequestConfig config=RequestConfig.custom().setConnectionRequestTimeout(1000)
                .setConnectionRequestTimeout(500).setSocketTimeout(10000).build();
        return config;
    }


}
